unit frm_FilterDebts;
 
interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, shbl_Filter, PropFilerEh, PropStorageEh, ActnList, Menus,
  StdCtrls, Buttons, ExtCtrls, DBCtrlsEh, DBGridEh, DBLookupEh, Mask, DB,
  FIBDataSet, pFIBDataSet, FIBDatabase, pFIBDatabase, ComCtrls;

type
  TfrmFilterDebts = class(TshblFilter)
    aEnableControl: TAction;
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    cbName1: TDBCheckBoxEh;
    cbStreet2: TDBCheckBoxEh;
    cbHouse3: TDBCheckBoxEh;
    cbApparts4: TDBCheckBoxEh;
    cbPhone5: TDBCheckBoxEh;
    cbNotes6: TDBCheckBoxEh;
    cbContragent10: TDBCheckBoxEh;
    cbCollector11: TDBCheckBoxEh;
    eName: TDBEditEh;
    eHouse: TDBEditEh;
    eApparts: TDBEditEh;
    ePhone: TDBEditEh;
    eNotes: TDBEditEh;
    eContragent: TDBLookupComboboxEh;
    eCollector: TDBLookupComboboxEh;
    cbPeriod: TDBCheckBoxEh;
    lcbType: TDBLookupComboboxEh;
    cbSpecFilter: TDBCheckBoxEh;
    lcbSpecFilter: TDBLookupComboboxEh;
    eStreet: TDBLookupComboboxEh;
    cbClosed12: TDBCheckBoxEh;
    cbIsDeleted13: TDBCheckBoxEh;
    cbMaxSumm9: TDBCheckBoxEh;
    cbMinSumm8: TDBCheckBoxEh;
    cbLastPayDate7: TDBCheckBoxEh;
    teLastPay: TDBDateTimeEditEh;
    eMinSumm: TDBNumberEditEh;
    eMaxSumm: TDBNumberEditEh;
    pnlTraits: TPanel;
    SrcLabel: TLabel;
    DstLabel: TLabel;
    IncludeBtn: TSpeedButton;
    IncAllBtn: TSpeedButton;
    ExcludeBtn: TSpeedButton;
    ExAllBtn: TSpeedButton;
    Panel3: TPanel;
    SrcList: TListBox;
    Panel4: TPanel;
    DstList: TListBox;
    cbTraits14: TDBCheckBoxEh;
    pClosed: TPanel;
    cbClosed: TRadioButton;
    RadioButton2: TRadioButton;
    pDeleted: TPanel;
    cbDeleted: TRadioButton;
    RadioButton3: TRadioButton;
    cbTown: TDBCheckBoxEh;
    eTown: TDBLookupComboboxEh;
    cbOrgs: TDBCheckBoxEh;
    lcbOrgs: TDBLookupComboboxEh;
    cbDogType: TDBCheckBoxEh;
    lcbDog_type: TDBLookupComboboxEh;
    cbDogNo: TDBCheckBoxEh;
    eDogno: TDBEditEh;
    cbKind: TDBCheckBoxEh;
    lcbKinds: TDBLookupComboboxEh;
    procedure IncludeBtnClick(Sender: TObject);
    procedure IncAllBtnClick(Sender: TObject);
    procedure ExcludeBtnClick(Sender: TObject);
    procedure ExAllBtnClick(Sender: TObject);
    procedure aFilterClearExecute(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure cbName1Click(Sender: TObject);
    procedure aApplyExecute(Sender: TObject);
    procedure cbSpecFilterKeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }
  public
    procedure FillListBox;
    procedure MoveSelected(List: TCustomListBox; Items: TStrings);
    procedure SetItem(List: TListBox; Index: Integer);
    function GetFirstSelection(List: TCustomListBox): Integer;
    procedure SetButtons;
  end;

var
  frmFilterDebts: TfrmFilterDebts;

implementation

uses MAIN_DM, MAIN, rst_Debts;

{$R *.dfm}

procedure TfrmFilterDebts.FillListBox;
begin
  if not MainDM.dsTraits.Active then exit;
  MainDM.dsTraits.First;
  while not MainDM.dsTraits.Eof do
  begin
    SrcList.AddItem(MainDM.dsTraitsNAME.AsString, TObject(MainDM.dsTraitsID.AsInteger));
    MainDM.dsTraits.Next;
  end;
end;

procedure TfrmFilterDebts.IncludeBtnClick(Sender: TObject);
var
  Index: Integer;
begin
  Index := GetFirstSelection(SrcList);
  MoveSelected(SrcList, DstList.Items);
  SetItem(SrcList, Index);
end;

procedure TfrmFilterDebts.ExcludeBtnClick(Sender: TObject);
var
  Index: Integer;
begin
  Index := GetFirstSelection(DstList);
  MoveSelected(DstList, SrcList.Items);
  SetItem(DstList, Index);
end;

procedure TfrmFilterDebts.IncAllBtnClick(Sender: TObject);
var
  I: Integer;
begin
  for I := 0 to SrcList.Items.Count - 1 do
    DstList.Items.AddObject(SrcList.Items[I],
      SrcList.Items.Objects[I]);
  SrcList.Items.Clear;
  SetItem(SrcList, 0);
end;

procedure TfrmFilterDebts.ExAllBtnClick(Sender: TObject);
var
  I: Integer;
begin
  for I := 0 to DstList.Items.Count - 1 do
    SrcList.Items.AddObject(DstList.Items[I], DstList.Items.Objects[I]);
  DstList.Items.Clear;
  SetItem(DstList, 0);
end;

procedure TfrmFilterDebts.MoveSelected(List: TCustomListBox; Items: TStrings);
var
  I: Integer;
begin
  for I := List.Items.Count - 1 downto 0 do
    if List.Selected[I] then
    begin
      Items.AddObject(List.Items[I], List.Items.Objects[I]);
      List.Items.Delete(I);
    end;
end;

procedure TfrmFilterDebts.SetButtons;
var
  SrcEmpty, DstEmpty: Boolean;
begin
  SrcEmpty := SrcList.Items.Count = 0;
  DstEmpty := DstList.Items.Count = 0;
  IncludeBtn.Enabled := not SrcEmpty;
  IncAllBtn.Enabled := not SrcEmpty;
  ExcludeBtn.Enabled := not DstEmpty;
  ExAllBtn.Enabled := not DstEmpty;
end;

function TfrmFilterDebts.GetFirstSelection(List: TCustomListBox): Integer;
begin
  for Result := 0 to List.Items.Count - 1 do
    if List.Selected[Result] then Exit;
  Result := LB_ERR;
end;

procedure TfrmFilterDebts.SetItem(List: TListBox; Index: Integer);
var
  MaxIndex: Integer;
begin
  with List do
  begin
    MaxIndex := List.Items.Count - 1;
    if Index = LB_ERR then Index := 0
    else if Index > MaxIndex then Index := MaxIndex;
    Selected[Index] := True;
  end;
  SetButtons;
end;

procedure TfrmFilterDebts.aFilterClearExecute(Sender: TObject);
begin
  ExAllBtnClick(nil);
  inherited;
end;

procedure TfrmFilterDebts.FormCreate(Sender: TObject);
begin
  MainDM.dsStreets.CloseOpen(true);
  MainDM.dsContragents.CloseOpen(true);
  MainDM.dsTraits.CloseOpen(true);
  FillListBox;
  SetButtons;
end;

procedure TfrmFilterDebts.cbName1Click(Sender: TObject);
begin
  EnableControlByTag(Self, (Sender as TWinControl), (Sender as TWinControl).Tag, (Sender as TDBCheckBoxEh).Checked);
end;

procedure TfrmFilterDebts.aApplyExecute(Sender: TObject);
begin
  Self.Close;
  (Self.Owner as TrstDebts).aFilterApply;
end;

procedure TfrmFilterDebts.cbSpecFilterKeyPress(Sender: TObject;
  var Key: Char);
begin
  inherited;
  if Key = #13 then
    Self.FindNextControl((Sender as TWinControl), true, true, false).SetFocus;
end;

end.
